package com.sunda.spmsoversea.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.entity.OverseaInvCheckDtl;
import com.sunda.spmsoversea.mapper.OverseaInvCheckDtlMapper;
import com.sunda.spmsoversea.service.IOverseaInvCheckDtlService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * <p>
 * 盘点任务表明细表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2021-05-10
 */
@Service
public class OverseaInvCheckDtlServiceImpl extends ServiceImpl<OverseaInvCheckDtlMapper, OverseaInvCheckDtl> implements IOverseaInvCheckDtlService {


    /*获取InventoryCheck行项*/
    @Override
    public ResponseResult getInventoryCheckDtl(String uuid) {
        if (StringUtils.isEmpty(uuid)) {
            return ResponseResult.error("uuid is mandatory required");
        }
        try {
            return ResponseResult.success().add("inventoryCheckDtls", this.baseMapper.selectInvDtl(uuid));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("failed when query Inventory Check Dtl");
        }
    }

    /*更新InventoryCheckDtl行项
     * 以uuid及taskRowId为key，存在则更新，不存在则新建
     * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateInventoryCheckDtl(String uuid, JSONArray invCheckDtls) {
        try{
            if (StringUtils.isEmpty(uuid)) {
                throw new Exception("uuid is mandatory required");
            }
            int taskRowId = this.baseMapper.selectMaxTaskRowId(uuid);
            for (int i = 0; i < invCheckDtls.size(); i++) {
                OverseaInvCheckDtl inventoryCheckDtl = invCheckDtls.getObject(i, OverseaInvCheckDtl.class);
                QueryWrapper<OverseaInvCheckDtl> invDtlQuerywarp = new QueryWrapper<OverseaInvCheckDtl>().eq("UUID", uuid).eq("TASK_ROW_ID", inventoryCheckDtl.getTaskRowId());
                List<OverseaInvCheckDtl> invDtls = this.baseMapper.selectList(invDtlQuerywarp);
                if (invDtls.size() > 0) {
                    // 存在则更新
                    this.baseMapper.update(inventoryCheckDtl, invDtlQuerywarp);
                } else {
                    // 不存在，新建
                    taskRowId += 10;
                    inventoryCheckDtl.setTaskRowId(String.valueOf(taskRowId));
                    inventoryCheckDtl.setUuid(uuid);
                    this.baseMapper.insert(inventoryCheckDtl);
                }
            }
            return ResponseResult.success().add("inventoryCheckDtls",this.baseMapper.selectInvDtl(uuid));
        }catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.error(e.getMessage());
        }
    }


}
